<?php
namespace Client\Controller;
use Think\Controller;
class NotifyController extends Controller {
    
	public function _initialize() {
		
	}
	
	
    public function index(){
    	
    }
	
	//微信支付回调
	public function wx_notify(){
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		$wx=new \bb\Wechat($options);
		$data=$wx->getNotify();
					
		if($data){
			$attach=explode('|',$data['attach']);
			$orderid=$attach['0'];
			$userid=$attach['1'];
			$m=M('bb_order');
			$order=$m->where(array('orderid'=>$orderid,'userid'=>$userid))->find();
			if($order){
				$order['is_pay']=1;
				$order['pay_price']=($data['total_fee']*0.01);
				$order['status']=3;
				$order['pay_type']='微信';
				$re=$m->save($order);
				if($re){
					
					//======================
					$userset=M('bb_user_set')->find();
					$consume_score=round($order['total_price']*$userset['consume_score']/100);
					$u_integral=M('client_user')->where(array('openid'=>$order['openid']))->find();
					$u_integral['integral']=($u_integral['integral']+$consume_score);
					M('client_user')->save($u_integral);
					
					
					
					
					
					//加入充值后提成
					$arr_f = M('bb_user_set')->find();
					$jjj_f = explode(',',$u_moeny['path']);
					
					if(count($jjj_f) >= 4){
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f[count($jjj_f)-1]))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx1cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx1cz']*0.01).'元！';
						M('log_msg')->add($log);
						
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f[count($jjj_f)-2]))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx2cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx2cz']*0.01).'元！';
						M('log_msg')->add($log);
						
						
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f[count($jjj_f)-3]))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx3cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx3cz']*0.01).'元！';
						M('log_msg')->add($log);
						
					}elseif(count($jjj_f) == 3){
						
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f['2']))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx1cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx1cz']*0.01).'元！';
						M('log_msg')->add($log);
						
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f['1']))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx2cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx2cz']*0.01).'元！';
						M('log_msg')->add($log);
						
					}elseif(count($jjj_f) == 2){
						
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f['1']))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx2cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx1cz']*0.01).'元！';
						M('log_msg')->add($log);
						
					}else{
						
					}
					
					
					
					
					
					
					
					
					
					
					$log['openid']=$order['openid'];
					$log['type']='1';   //积分日志
					$log['addtime']=time();   
					$log['msg']='积分增加：下单获得'.$consume_score.'积分';
					M('log_msg')->add($log);
					//=========================

					$wx->returnNotify();
				}
			}
		}
	}

	//微信充值回调
	public function wx_cznotify(){
		$options['appid']=C('BB_APPID_client');
		$options['secret']=C('BB_SECRET_client');
		$wx=new \bb\Wechat($options);
		$data=$wx->getNotify();
					
		if($data){
			$attach=explode('|',$data['attach']);
			$czid=$attach['0'];
			$openid=$attach['1'];
			$usercz=M('usercz_log')->where(array('czid'=>$czid,'openid'=>$openid))->find();
			if($usercz){
				$usercz['status']=1;
				$re=M('usercz_log')->save($usercz);
				if($re){
					$u_moeny=M('client_user')->where(array('openid'=>$openid))->find();
					$u_moeny['money']=($u_moeny['money']+($data['total_fee']*0.01));
					M('client_user')->save($u_moeny);
					
					//加入充值后提成
					$arr_f = M('bb_user_set')->find();
					$jjj_f = explode(',',$u_moeny['path']);
					
					if(count($jjj_f) >= 4){
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f[count($jjj_f)-1]))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx1cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx1cz']*0.01).'元！';
						M('log_msg')->add($log);
						
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f[count($jjj_f)-2]))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx2cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx2cz']*0.01).'元！';
						M('log_msg')->add($log);
						
						
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f[count($jjj_f)-3]))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx3cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx3cz']*0.01).'元！';
						M('log_msg')->add($log);
						
					}elseif(count($jjj_f) == 3){
						
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f['2']))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx1cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx1cz']*0.01).'元！';
						M('log_msg')->add($log);
						
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f['1']))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx2cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx2cz']*0.01).'元！';
						M('log_msg')->add($log);
						
					}elseif(count($jjj_f) == 2){
						
						$u_moeny_f=M('client_user')->where(array('id'=>$jjj_f['1']))->find();
						$u_moeny_f['money']=($u_moeny_f['money']+($data['total_fee']*0.01*$arr_f['fx2cz']*0.01));
						M('client_user')->save($u_moeny_f);
						
						$log['openid']=$u_moeny_f['openid'];
						$log['type']='3';   //RMB
						$log['addtime']=time();   
						$log['msg']=$u_moeny['username'].'用户充值后提成 '.($data['total_fee']*0.01*$arr_f['fx1cz']*0.01).'元！';
						M('log_msg')->add($log);
						
					}else{
						
					}
					
					
				}
			}
		}
		
	}

	//支付宝支付回调
	public function zfb_notify(){
		echo '支付成功！';
	}
	
	//支付中断或者用户取消支付
	public function zfb_zhongduan(){
		echo '支付取消！';
	}
	//支付宝支付异步回调
	public function zfb_notify_yb(){
		foreach($_POST as $k=>$v){
				$str=$str.$k."=>".$v.'  ';
			}
		file_put_contents('zfh_nd.txt','响应参数:'.date('Y-m-d H:i:s').PHP_EOL.'通知信息:'.$str.PHP_EOL.PHP_EOL.PHP_EOL,FILE_APPEND);
		
		$doc = new \DOMDocument();
		$doc->loadXML($_POST['notify_data']);
		//商户订单号
		$out_trade_no = $doc->getElementsByTagName( "out_trade_no" )->item(0)->nodeValue;
		//支付宝交易号
		$trade_no = $doc->getElementsByTagName( "trade_no" )->item(0)->nodeValue;
		//交易状态
		$trade_status = $doc->getElementsByTagName( "trade_status" )->item(0)->nodeValue;
		//买家账号
		$buyer_email = $doc->getElementsByTagName( "buyer_email" )->item(0)->nodeValue;
		//付款金额
		$total_fee = $doc->getElementsByTagName( "total_fee" )->item(0)->nodeValue;
		
		if ($trade_status== 'TRADE_SUCCESS') {
			$res=$this->zfb_ok_order($out_trade_no,$total_fee);
		}
	}
	
	//支付宝支付成功修改订单
	public function zfb_ok_order($orderid,$total_fee){
		$m=M('bb_order');
		$order=$m->where(array('orderid'=>$orderid))->find();
		if($order['is_pay']!=1){
			$order['is_pay']=1;
			$order['pay_price']=$total_fee;
			$order['status']=3;
			$order['pay_type']='支付宝';
			$re=$m->save($order);
			if($re){
				
					//======================
					$userset=M('bb_user_set')->find();
					$consume_score=round($order['total_price']*$userset['consume_score']/100);
					$u_integral=M('client_user')->where(array('openid'=>$order['openid']))->find();
					$u_integral['integral']=($u_integral['integral']+$consume_score);
					M('client_user')->save($u_integral);
					
					$log['openid']=$order['openid'];
					$log['type']='1';   //积分日志
					$log['addtime']=time();   
					$log['msg']='积分增加：下单获得'.$consume_score.'积分';
					M('log_msg')->add($log);
					//=========================
				
				return true;
			}else{
				return FALSE;
			}
		}
	}
}